/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.SurveyUserDTO;

/**
 *
 * @author User
 */
public class SurveyUserServiceImpl implements IEntity {
	
	Statement stmt=null;
    ResultSet rst=null;
    private SurveyUserDTO surveyUser;
    private List<SurveyUserDTO> surveyUserList;

    public SurveyUserDTO GetById(int id) {
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from surveyuser where idSurveyUser=" + id);
            surveyUser.setIdSurveyUser(id);
            surveyUser.setIdUser(rst.getInt("idUser"));
            surveyUser.setIdSurvey(rst.getInt("idSurvey"));
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(SurveyUserServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return surveyUser;
    }

    public List<SurveyUserDTO> GetAll() {
        try{
        	
        	stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from surveyuser ");
            while (rst.next()) {
            	surveyUser.setIdSurveyUser(rst.getInt("idSurveyUser"));
            	surveyUser.setIdUser(rst.getInt("idUser"));
            	surveyUser.setIdSurvey(rst.getInt("idSurvey"));
            	surveyUserList.add(surveyUser);
            }
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
        	
            Logger.getLogger(SurveyUserServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        return surveyUserList;
    }

    public void Add(Object obj) {	
    	
    	try {
    		
    		SurveyUserDTO objCast=(SurveyUserDTO)obj;
    		stmt =  Configuration.GetStatement();
    		stmt.execute("INSERT INTO dbsurvey.surveyuser (`idSurveyUser`, `idUser`, `idSurvey`)  VALUES ("+objCast.getIdSurveyUser()+","+objCast.getIdUser()+","+objCast.getIdSurvey()+")");
    		Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Update(Object obj) {
    	
    	try {
    		
    		SurveyUserDTO objCast=(SurveyUserDTO)obj;
    		stmt = Configuration.GetStatement();
    		stmt.execute("update dbsurvey.surveyuser set   idUser="+objCast.getIdUser()+"idSurvey="+objCast.getIdSurvey()+" where idSurveyUser="+objCast.getIdSurveyUser());
    		Configuration.CloseConnection();
        
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Remove(int id){
    	try {
    		
    		stmt = Configuration.GetStatement();
    		stmt.execute("delete from surveyuser where idSurveyUser =" + id);
    		Configuration.CloseConnection();
        
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
